package edu.uclmgmu.bacteriosec.aspects.common;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Vector;

import edu.uclmgmu.bacteriosec.rmiserver.IListener;

public class RMIStore {
	
	private static String url="rmi://127.0.0.1:2995/bacteriosecListener";

	/***
	 * Executed by *_CountCalls
	 * @param signature
	 */
	public static void updateNumberOfCalls(String joinPointName) {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			listener.updateNumberOfCalls(joinPointName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static int getNumberOfCallInTurn(String joinPointName) {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			return listener.getNumberOfCallInTurn(joinPointName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	public static int getNumberOfCurrentCall(String joinPointName) {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			return listener.getNumberOfCurrentCall(joinPointName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	public static void updateNumberOfCurrentCall(String joinPointName) {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			listener.updateNumberOfCurrentCall(joinPointName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void log(String canonicalName, int numberOfCurrentCall, String signature) {
//		String text="Operator: " + canonicalName + "\nCurrent call: " + numberOfCurrentCall + "\nJoin point:" + signature + "\n\n";
//		System.out.println(text);
	}

	public static String getCurrentjoinPoint() {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			return listener.getCurrentjoinPoint();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public static int getNumberOfCallsToJP(String jpFile) {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			return listener.getNumberOfCallsToJP(jpFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	public static void saveCurrentJoinPoint(String jpName) {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			listener.saveCurrentJoinPoint(jpName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void createFileForCallInTurn(String joinPointName, int callInTurn) throws IOException {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			listener.createFileForCallInTurn(joinPointName, callInTurn);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void saveAOP_AJC_XML(String aopXMLPath, String text) throws IOException {
		FileOutputStream fos=new FileOutputStream(aopXMLPath);
		fos.write(text.getBytes());
		fos.close();
	}

	public static int readNumberOfCalls(String jpName) throws IOException {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			return listener.readNumberOfCalls(jpName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return -1;
	}
	
	public static void createFileForCurrentCall(String joinPointName, int currentCall) throws IOException {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			listener.createFileForCurrentCall(joinPointName, currentCall);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Vector<String> loadFilesNumberOfCalls() {
		try {
			IListener listener=(IListener) Naming.lookup(RMIStore.url);
			return listener.loadFilesNumberOfCalls();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


}
